/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.crud.Impl;

import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.AlbumCategory;
import com.startup.musicstore.services.crud.AlbumCategoryCrudService;
import com.startup.musicstore.services.crud.MostSalesService;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author given
 */
public class MostSalesServiceImpl implements MostSalesService{
    private static ApplicationContext ctx;
    
    @Override
    public String getAlbumCategoryName() {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");
        int initialTotalSales = 0;
        String categoryName = "";
        AlbumCategoryCrudService albumCategoryCrudService = (AlbumCategoryCrudService) ctx.getBean("albumCategoryCrudService");
        List<AlbumCategory> albumCategorys =albumCategoryCrudService.findAll();
        for(AlbumCategory ac: albumCategorys){
            int totalSales = 0;
            List<Album> albums = ac.getAlbums();
            for(Album a: albums){
                totalSales += a.getSales();
            }
            if(initialTotalSales < totalSales){
                initialTotalSales = totalSales;
                categoryName = ac.getCategoryName();
            }
        }
        return categoryName;
    }    
}
